// 类的实现 继承
// 类必须使用new 调用，否则会报错

class Person {

}

// ------------------------
// 实现
// ------------------------
// 1.验证不能new
function _instanceof(left, right) {
    if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
        return !!right[Symbol.hasInstance](left)
    } else {
        return left instanceof right
    }
}

function _classCallCheck(instance, Constructor) {
    if (!_instanceof(instance, Constructor)) {
        throw new TypeError("connot call a class as a function")
    }
}

let Person = function Person() {
    _classCallCheck(this, Person)
}

// ---------------------------

class Person {
    constructor(name, age) {
        this.name = name;
        this.age = age;
    }
    eat() {
        return 'eat'
    }
    static say() {
        return 'say'
    }
}

// 2.实现属性和静态方法
function _instanceof(left, right) {
    if (right != null && typeof Symbol != undefined && right[Symbol.hasInstance]) {
        return right[Symbol.hasInstance][left]
    } else {
        return left instanceof right
    }
}

function _classCallCheck(instance, constructor) {
    if (_instanceof(instance, constructor)) {
        throw new TypeError("can not call a class as a function")
    }
}

function _defineProperties(target, props) {
    for (let i = 0; i < props.length; i++) {
        var descriptor = props[i];
        descriptor.enumerable = descriptor.enumerable || false;
        descriptor.configurable = true;  // 属性必须可被修改
        if ("value" in descriptor) descriptor.writable = true; // value 可被修改
        Object.defineProperty(target, descriptor.key, descriptor)
    }
}

function _createClass(constructor, protoProps, staticProps) {
    if (protoProps) _defineProperties(constructor.protoType, protoProps);
    if (staticProps) _defineProperties(constructor, staticProps)
    return constructor
}

var Person = function () {
    function Person() {
        _classCallCheck(this, Person)

    }
    _createClass(
        Person,
        [{
            key: "eat",
            value: function eat() { return "eat" }
        }],
        [{
            key: "say",
            value: function say() { return "say" }
        }]
    )
}


// 实现继承
function _inherits(child, _parent) {
    // var fatherPrototype = Object.create(_parent.prototype);
    // child.prototype = fatherPrototype;
    // child.prototype.constructor = child; 

    if (typeof _parent !== "function" && _parent !== null) {
        throw new TypeError("super expression must either be null or a function not " + typeof _parent)
    }

    child.protoType = Object.create(_parent && _parent.protoType, {
        constructor: {
            value: child,
            enumerable: false,
            writable: true,
            configurable: true
        }
    })

    if (_parent) {
        Object.setPrototypeOf
            ? Object.setPrototypeOf(child, _parent)
            : (child.__proto__ = _parent)
    }
}

function _prossibleConstructorReturn(self, call) {
    if (!self) { throw new ReferenceError("this hasn't been initialised super() hasn't been called") }
    return call && (typeof call === "object" || typeof call === "function") ? call : self;
}


var Child = (function (_Parent) {
    _inherits(Child, _Parent);
    function Child(name, age) {
        _classCallCheck(this, Child)

        // 新增部分
        // _Parent.call(this);
        var _this = _prossibleConstructorReturn(
            this,
            (Child.__proto__ || Object.getPrototypeOf(Child)).call(this, name)
        )
        _this.age = age;
        return _this;
    }
    _createClass(Child, [{
        key: "sayHello",
        value: function sayHello() {
            console.log("...." + this.age)
        }
    }])

    return Child;
})(Person)
